package com.itsession.blog.core.controller.blog;

import com.itsession.blog.core.entity.AdminUser;
import com.itsession.blog.core.service.AdminUserService;
import com.itsession.blog.core.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @program: Blog
 * @ClassName: PcBlogLogiinController
 * @description: 用户登录
 * @author: hanP
 * @create: 2021-12-29 09:02
 **/
@Controller
@RequestMapping("/pc")
public class PcBlogLogiinController {

    @Resource
    private AdminUserService adminUserService;


    @GetMapping({"/login"})
    public String login() {
        return "blog/default/login";
    }


    @PostMapping(value = "/login")
    public String login(@RequestParam("userName") String userName,
                        @RequestParam("password") String password,
                        @RequestParam("verifyCode") String verifyCode,
                        HttpSession session) {
        if (StringUtils.isEmpty(verifyCode)) {
            session.setAttribute("errorMsg", "验证码不能为空");
            return "blog/default/login";
        }
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "blog/default/login";
        }
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
            session.setAttribute("errorMsg", "验证码错误");
            return "blog/default/login";
        }

        AdminUser adminUser = adminUserService.login(userName, password);
        if (adminUser != null) {
            session.setAttribute("pcLoginUser", adminUser.getNickName());
            session.setAttribute("pcLoginUserId", adminUser.getAdminUserId());
            //session过期时间设置为7200秒 即两小时
            //session.setMaxInactiveInterval(60 * 60 * 2);
            return "redirect:/index";
        } else {
            session.setAttribute("errorMsg", "登陆失败");
            return "blog/default/login";
        }
    }


    @GetMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("pcLoginUser");
        request.getSession().removeAttribute("pcLoginUserId");
        request.getSession().removeAttribute("errorMsg");
        return "blog/default/login";
    }

    @GetMapping({"/register"})
    public String register() {
        return "blog/default/register";
    }


    @PostMapping(value = "/register")
    public String register(@RequestParam("userName") String userName,
                           @RequestParam("password") String password,
                           @RequestParam("nickName") String nickName,
                           HttpSession session) {
        if (StringUtils.isEmpty(nickName)) {
            session.setAttribute("errorMsg", "昵称不能为空");
            return "blog/default/register";
        }
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "blog/default/register";
        }

        AdminUser adminUser = new AdminUser();
        adminUser.setNickName(nickName);
        adminUser.setLoginUserName(userName);
        String passwordMd5 = MD5Util.MD5Encode(password, "UTF-8");
        adminUser.setLoginPassword(passwordMd5);
        adminUserService.saveAdminUser(adminUser);
        return "redirect:/pc/login";
    }

}
